package com.behelpful.demo.admin.controller;

import cn.hutool.core.util.IdUtil;
import com.behelpful.common.core.result.R;
import com.behelpful.common.integration.captcha.model.CaptchaVo;
import com.behelpful.common.integration.captcha.service.CaptchaService;
import com.behelpful.common.integration.iredis.constants.ValidateCodeEnum;
import com.behelpful.common.integration.iredis.support.IRedisUtil;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

/**
 * 验证码服务管理
 *
 * @author Alay
 * @date 2021-09-22 13:14
 */
@RestController
@RequestMapping(value = "/captcha")
@Api(value = "captcha", tags = "验证码服务管理")
public class CaptchaController {
    @Autowired
    private CaptchaService captchaService;
    @Autowired
    private IRedisUtil iRedisUtil;


    //@OpenApi
    @ApiOperation(value = "获取算数验证码", notes = "获取算数验证码")
    @GetMapping(value = "/getArithmeticCode")
    public R getArithmeticCode() {
        CaptchaVo captchaVo = captchaService.genArithmeticCaptcha(111, 36, 2);
        long nextId = IdUtil.getSnowflake(1, 1).nextId();
        captchaVo.setCodeId(nextId + "");
        // 验证码存入Redis
        iRedisUtil.saveValidateCode(ValidateCodeEnum.LOGIN_VALIDATE, captchaVo.getCodeId(), captchaVo.getResult(), false);
        return R.ok(captchaVo);
    }
}
